package net.butfly.bus.context;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.butfly.bus.util.LogUtils;

public class Constants {
	public static final String coreConfigXmlFile = "ebox-core.xml";

	public static final String EXT_EBASE_ENUMFACTORY = "net.butfly.bus.util.io.EnumFactory";
	public static final String logName = "net.butfly.bus";
	private final static String ERROR_PREFIX = "BUS_";

	public interface SystemError {// 000-099
		String UNKNOW_CAUSE = ERROR_PREFIX + "000";// 未知异常
		String CONFIG_ERROR = ERROR_PREFIX + "001";// 配置错误
		String HESSIAN_UNKNOW = ERROR_PREFIX + "002";// Hessian未知异常
		String HESSIAN_MALFORMED_URL = ERROR_PREFIX + "003";// 调用Hessian时应用了错误的URL格式
		String HESSIAN_CONNECTION = ERROR_PREFIX + "004";// Hessian连接错误
		String ROUTE_NOT_FOUND = ERROR_PREFIX + "005";// 路由节点失败
		String TIMEOUT_FOR_IO = ERROR_PREFIX + "007";// IO超时
		String TIMEOUT_FOR_WAIT = ERROR_PREFIX + "008";// 等待超时错误
		String TIMEOUT_CAN_NOT_STOP = ERROR_PREFIX + "009";// 超时中断线程错误
		String THREAD_COUNTER_OVERFLOW = ERROR_PREFIX + "010";// 并发线程数超出配置
	}

	public interface UserError {// 100-199
		String BAD_REQUEST = ERROR_PREFIX + "100";// 请求错误
		String USER_NOT_EXIST = ERROR_PREFIX + "101";// 用户不存在
		String WRONG_PASSWORD = ERROR_PREFIX + "102";// 密码错误
		String NODE_CONFIG = ERROR_PREFIX + "104";// 节点配置错误
		String NODE_NOT_FOUND = ERROR_PREFIX + "105";// 没有找到配置的节点
		String NODE_CONNECTION_NOT_FOUND = ERROR_PREFIX + "106";// 节点没有配置连接
		String TX_CONFIG = ERROR_PREFIX + "107";// 交易配置错误
		String TX_NOT_FOUND = ERROR_PREFIX + "108";// 交易没有找到
		String TX_NOT_EXIST = ERROR_PREFIX + "109";// 交易不存在
		String FILTER_CONFIG = ERROR_PREFIX + "111";// 过滤器配置错误
		String FILTER_INSTANCE = ERROR_PREFIX + "112";// 过滤器实例化错误
		String FILTER_INIT = ERROR_PREFIX + "113";// 过滤器初始化错误
		String FILTER_INVOKE = ERROR_PREFIX + "114";// 过滤器调用错误
		String CONFIG_ERROR = ERROR_PREFIX + "115";// 文件配置错误
	}

	public interface BusinessError {// 200-299
		String CONFIG_ERROR = ERROR_PREFIX + "200";// 业务逻辑配置错误
		String BUSI_INSTANCE = ERROR_PREFIX + "201";// 实例化错误
		String INVOKE_TIMEOUT = ERROR_PREFIX + "202";// 调用超时
		String INVOKE_ERROR = ERROR_PREFIX + "203";// 调用错误
		String BUSI_NOT_FOUND = ERROR_PREFIX + "204";// 业务逻辑不存在
	}

	// --------------------------------------------------------------------------------

	public static String getFlowNo(String tx, String version) {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + LogUtils.getRandomId(7) + "@" + tx + null == version ? ""
				: "[" + version + "]";
	}

	public static URL getCoreConfigFile() {
		return Constants.class.getResource(coreConfigXmlFile);
	}
}
